package cn.shop.admin.controller;

import cn.shop.admin.entity.businessCooperationModule.BusinessCooperation;
import cn.shop.admin.service.BusinessCooperationService;
import cn.shop.base.MyPage;
import cn.shop.utils.ApiResult;
import cn.shop.validation.Save;
import cn.shop.validation.Update;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 商务合作(BusinessCooperation)表控制层
 *
 * @author 痴
 * @since 2022-06-02 16:12:46
 */
@Validated
@RestController
@RequestMapping("/api/v1/businessCooperation")
public class BusinessCooperationController {

	/**
	 * 服务对象
	 */
	@Resource
	private BusinessCooperationService businessCooperationService;

	/**
	 * 查询所有数据
	 *
	 * @param businessCooperation 查询实体
	 * @return 所有数据
	 * @author 痴
	 * @since 2022-06-02 16:12:46
	 */
	@GetMapping("queryAll")
	public ApiResult<List<BusinessCooperation>> queryAll(BusinessCooperation businessCooperation) {
		return ApiResult.success(businessCooperationService.list(new QueryWrapper<>(businessCooperation)));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page                分页对象
	 * @param businessCooperation 查询实体
	 * @return 所有数据
	 * @author 痴
	 * @since 2022-06-02 16:12:46
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<BusinessCooperation>> queryByPage(MyPage<BusinessCooperation> page, BusinessCooperation businessCooperation) {
		page.addOrder(OrderItem.desc("status"));
		page.addOrder(OrderItem.desc("id"));
		return ApiResult.success(businessCooperationService.page(page, new QueryWrapper<>(businessCooperation)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author 痴
	 * @since 2022-06-02 16:12:46
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<BusinessCooperation> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(businessCooperationService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param businessCooperation 实体对象
	 * @return 新增结果
	 * @author 痴
	 * @since 2022-06-02 16:12:46
	 */
	@PostMapping("insert")
	public ApiResult<Map<String, Object>> insert(@Validated(Save.class) @RequestBody BusinessCooperation businessCooperation) {
		return ApiResult.result(businessCooperationService.save(businessCooperation));
	}

	/**
	 * 修改数据
	 *
	 * @param businessCooperation 实体对象
	 * @return 修改结果
	 * @author 痴
	 * @since 2022-06-02 16:12:46
	 */
	@PutMapping("update")
	public ApiResult<Map<String, Object>> update(@Validated(Update.class) @RequestBody BusinessCooperation businessCooperation) {
		return ApiResult.result(businessCooperationService.updateById(businessCooperation));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author 痴
	 * @since 2022-06-02 16:12:46
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		BusinessCooperation businessCooperation = new BusinessCooperation();
		businessCooperation.setId(id);
		return ApiResult.result(businessCooperationService.removeById(businessCooperation));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author 痴
	 * @since 2022-06-02 16:12:46
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		return ApiResult.result(businessCooperationService.batchDelete(idList, new BusinessCooperation()));
	}

}

